<?php
/**
 * FastApp.
 * 8/22/24
 * @Author deepsea159
 * @Contact telegram:deepsea159
 */

namespace App\Game\Job;

use App\Marketing\Model\MarketingLotto;
use App\Marketing\Model\MarketingLottoLog;
use Hyperf\AsyncQueue\Job;

class LottoJob extends Job
{

    public function __construct(public int $data_id, public int $periods, public string $no)
    {
    }

    public function handle(): void
    {
        $res = MarketingLottoLog::query()
            ->where(['lotto_id' => $this->data_id, 'periods' => $this->periods])
            ->orderBy('id')->get();
        $open_no = explode(' ', $this->no);
        $config = MarketingLotto::query()->where(['id' => $this->data_id])->value('config');
        $level_num = array_column($config, 'num', 'level');
        $win_level_num = [];
        foreach ($res as $log) {
            foreach ($config as $c) {
                if (compareArraysAtIndexes($open_no, explode(' ', $log->no), $c['match'])) {
                    if (isset($level_num[$c['level']])) {
                        if (!isset($win_level_num[$c['level']])) $win_level_num[$c['level']] = 0;
                        if ($win_level_num[$c['level']] >= $level_num[$c['level']]) break;
                        $win_level_num[$c['level']]++;
                    }
                    $log->update(['level' => $c['level'], 'amount' => $c['amount']]);
                    break;
                }
            }
        }
    }

}